#coding:utf-8
from django.http import HttpResponseRedirect, Http404, HttpResponse
from django.shortcuts import render
from Nodecontrol.models import nodegroup
from django.contrib.auth.decorators import login_required
from forms import AddForm
import urllib2
import json

@login_required(login_url="/login/")
def area_view(request):
    if request.method == 'POST':# 当提交表单时

        form_info = AddForm(request.POST) # form 包含提交的数据

        if form_info.is_valid():# 如果提交的数据合法
            #form = form_info.cleaned_data
            form_info.save()
            return HttpResponseRedirect('/area')
        else:
            return HttpResponse(json.dumps({"success":True}), content_type="application/json") #这里看下怎么抛错#http://stackoverflow.com/questions/27481308/django-bootstrap-modal-after-form-submission

    else:# 当正常访问时
        form_info = AddForm()
    #加载界面的时候请求一次

    area_list = nodegroup.objects.all()
    print area_list

    return render(request, 'area_view.html',locals())